﻿// A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
// 用于判断是HTML还是ID字符串
var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/;

// Is it a simple selector
// 简单的选择器
var isSimple = /^.[^:#\[\.]*$/;
var Core = Class.create();
Core.prototype = {
    _construct: function(selector, context){
        // Make sure that a selection was provided
        //确认选择器
		selector = selector || document;

		// Handle $(DOMElement)
		//如果选择器是一个对象
		if ( selector.nodeType ) {
			this[0] = selector;
			this.length = 1;
			return this;

		// Handle HTML strings
		//如果是HTML字符串
		} else if ( typeof selector == "string" ) {
			// Are we dealing with HTML string or an ID?
			//用来判断是HTML还是ID
			var match = quickExpr.exec( selector );

			// Verify a match, and that no context was specified for #id
			//校验匹配结果，
			if ( match && (match[1] || !context) ) {

				// HANDLE: $(html) -> $(array)
				if ( match[1] ){
					//selector = jQuery.clean( [ match[1] ], context );
                
				// HANDLE: $("#id")
				}else {
					var elem = document.getElementById( match[3] );

					// Make sure an element was located
					if ( elem )
						// Handle the case where IE and Opera return items
						// by name instead of ID
						//在IE 和 Opera 中有可能返回的是name而非ID匹配的元素
						if ( elem.id != match[3] )
							return Ctn().find( selector );

						// Otherwise, we inject the element directly into the jQuery object
						else {
							this[0] = elem;
							this.length = 1;
							return this;
						}

					else
						selector = [];
				}

			// HANDLE: $(expr, [context])
			// (which is just equivalent to: $(content).find(expr)
			} else
				return new jQuery( context ).find( selector );

		// HANDLE: $(function)
		// Shortcut for document ready
		}else if ( jQuery.isFunction( selector ) )
			return new jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector );

		return this.setArray(
			// HANDLE: $(array)
			selector.constructor == Array && selector ||

			// HANDLE: $(arraylike)
			// Watch for when an array-like object, contains DOM nodes, is passed in as the selector
			//查找包含DOM节点的类数组对象
			(selector.jquery || selector.length && selector != window && !selector.nodeType && selector[0] != undefined && selector[0].nodeType) && new Array().from( selector ) ||

			// HANDLE: $(*)
			[ selector ] );
    },
    // The current version of jQuery being used
    //当前版本
	jquery: "1.2.3",
	
	// The number of elements contained in the matched element set
	//符合匹配的元素个数
	length: 0,

	// Get the Nth element in the matched element set OR
	// Get the whole matched element set as a clean array
	//获取符合匹配的单个元素或数组
	get: function( num ) {
		///<summary>note: lookup this function</summary>
		///<returns type="jQuery" />
		
	
		return num == undefined ?
		///<summary>Access all matched DOM elements. note: intellisense not working.</summary>
		///<param name="num" type="Number" optional="true">(optional) Access the element in the Nth position.</param>
		///<returns type="Array" />

			// Return a 'clean' array
			new Array().from( this ) :

			// Return just the object
			this[ num ];
	},
	
	// Take an array of elements and push it onto the stack
	// (returning the new matched element set)
	// 将元素放入堆栈中
	pushStack: function( elems ) {
		// Build a new jQuery matched element set
		var ret = jQuery( elems );

		// Add the old object onto the stack (as a reference)
		ret.prevObject = this;

		// Return the newly-formed element set
		return ret;
	},
	
	// Force the current matched set of elements to become
	// the specified array of elements (destroying the stack in the process)
	// You should use pushStack() in order to do this, but maintain the stack
	//放入数组中
	setArray: function( elems ) {
		// Resetting the length to 0, then using the native Array push
		// is a super-fast way to populate an object with array-like properties
		// 重设数组，直接将长度设为0，并调用原生的push来构造
		this.length = 0;
		Array.prototype.push.apply( this, elems );
		
		return this;
	},

	// Execute a callback for every element in the matched set.
	// (You can seed the arguments with an array of args, but this is
	// only used internally.)
	//遍历匹配的元素，
	each: function( callback, args ) {
		///<summary>Execute a function within the context of every matched element.</summary>
		///<param name="callback">The callback to execute for each matched element.</param>
		///<param name="args">(You can seed the arguments with an array of args, but this is only used internally.)</param>
		///<returns type="jQuery" />

		return Ctn.util.each( this, callback, args );
	},

	// Determine the position of an element within 
	// the matched set of elements
	//查找已匹配的元素位置
	index: function( elem ) {
		///<summary>Searches every matched element for the object and returns the index of the element, if found, starting with zero.</summary>
		///<param name="elem">Object to search for.</param>
		///<returns type="Number" />
		var ret = -1;

		// Locate the position of the desired element
		this.each(function(i){
			if ( this == elem )
				ret = i;
		});

		return ret;
	}
}
